package com.resource.authoritymanage.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.resource.authoritymanage.config.TIdentifyRecordEntityListener;
import com.resource.authoritymanage.config.TTeacherInformationListener;
import com.resource.authoritymanage.entity.TIdentifyRecordEntity;
import com.resource.authoritymanage.entity.TIdentifyRecordEntityExcel;
import com.resource.authoritymanage.entity.TTeacherInformationEntity;
import com.resource.authoritymanage.entity.TTeacherInformationEntityExcel;
import com.resource.authoritymanage.mapper.TTeacherInformationDao;
import com.resource.authoritymanage.service.TTeacherInformationService;
import com.resource.common.utils.AppResponse;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.Query;
import com.resource.common.utils.R;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


@Service
public class TTeacherInformationServiceImpl extends ServiceImpl<TTeacherInformationDao, TTeacherInformationEntity> implements TTeacherInformationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int size = params.size();
        IPage<TTeacherInformationEntity> page = new Page<>();
        if (size == 4) {
            String keys = "";
            String param = "";
            Set<String> strings = params.keySet();
            for (String key : strings) {
                keys = key;
                param = params.get(keys).toString();
            }
            page = this.page(
                    new Query<TTeacherInformationEntity>().getPage(params),
                    new QueryWrapper<TTeacherInformationEntity>().eq(keys, param)
            );
            return new PageUtils(page);
        }
        page = this.page(
                new Query<TTeacherInformationEntity>().getPage(params),
                new QueryWrapper<TTeacherInformationEntity>());
        return new PageUtils(page);
    }

    @Override
    public R importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),TTeacherInformationEntityExcel.class,new TTeacherInformationListener(baseMapper)).sheet("教师信息表").doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  R.ok();
    }

    @Override
    public R exportData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = "teacherInformation";
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            List<TTeacherInformationEntity> tTeacherInformationEntities = this.getBaseMapper().selectList(new QueryWrapper<TTeacherInformationEntity>());
            List<TTeacherInformationEntityExcel> list = new ArrayList<>();
            int sortNo = 1;
            for (TTeacherInformationEntity tTeacherInformationEntity : tTeacherInformationEntities) {
                TTeacherInformationEntityExcel tTeacherInformationEntityExcel = new TTeacherInformationEntityExcel();
                tTeacherInformationEntityExcel.setSortNo(sortNo++ + "");
                tTeacherInformationEntityExcel.setTeacherAge(tTeacherInformationEntity.getTeacherAge());
                tTeacherInformationEntityExcel.setTeacherSex(tTeacherInformationEntity.getTeacherSex());
                tTeacherInformationEntityExcel.setTeacherName(tTeacherInformationEntity.getTeacherName());
                tTeacherInformationEntityExcel.setTeacherCode(tTeacherInformationEntity.getTeacherCode());
                tTeacherInformationEntityExcel.setTeachingCourseName(tTeacherInformationEntity.getTeachingCourseName());
                tTeacherInformationEntityExcel.setTeachingCourseCode(tTeacherInformationEntity.getTeachingCourseCode());
                list.add(tTeacherInformationEntityExcel);
            }
            EasyExcel.write(response.getOutputStream(), TTeacherInformationEntityExcel.class).sheet("教师信息表").doWrite(list);
            return R.ok("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("导出失败");
    }
}